<template>
  <div>
    <div style="display: flex; justify-content: space-between">
      <div>
        <el-input
          v-model="empName"
          placeholder="输入员工姓名搜索..."
          prefix-icon="el-icon-search"
          style="width: 300px; margin-right: 10px"
          clearable
          @clear="initEmps"
          @keydown.enter.native="initEmps"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="initEmps">搜索</el-button>
        <el-button type="primary">
          <i class="fa fa-angle-double-down" aria-hidden="true"></i>
          高级搜索
        </el-button>
      </div>
      <div>
        <el-upload
          action="/employee/basic/import"
          :headers="headers"
          style="display: inline-flex; margin-right: 8px"
          :show-file-list="false"
          :before-upload="beforeUpload"
          :on-success="onSuccess"
          :disabled="importDataDisabled"
          :on-error="onError"
        >
          <el-button type="success" :icon="importDataBtnIcon">{{ importDataBtnText }}</el-button>
        </el-upload>
        <el-button type="success" @click="exportData" icon="el-icon-download">导出数据</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="showAddEmpView">添加员工</el-button>
      </div>
    </div>
    <div style="margin-top: 10px">
      <el-table
        :data="emps"
        stripe
        border
        style="width: 100%"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="name" label="姓名" width="90" align="left" fixed></el-table-column>
        <el-table-column prop="workID" label="工号" width="90" align="left"></el-table-column>
        <el-table-column prop="gender" label="性别" width="90" align="left"></el-table-column>
        <el-table-column prop="name" label="姓名" width="90" align="left"></el-table-column>
        <el-table-column prop="birthday" label="生日" width="90" align="left"></el-table-column>
        <el-table-column prop="idCard" label="身份证号" width="150" align="left"></el-table-column>
        <el-table-column prop="wedlock" label="婚姻状况" width="90" align="left"></el-table-column>
        <el-table-column prop="nation.name" label="民族" width="90" align="left"></el-table-column>
        <el-table-column prop="nativePlace" label="籍贯" width="90" align="left"></el-table-column>
        <el-table-column prop="politicsStatus.name" label="政治面貌" width="100" align="left"></el-table-column>
        <el-table-column prop="email" label="电子邮件" width="180" align="left"></el-table-column>
        <el-table-column prop="phone" label="电话号码" width="150" align="left"></el-table-column>
        <el-table-column prop="address" label="联系地址" width="250" align="left"></el-table-column>
        <el-table-column prop="department.name" label="所属部门" width="90" align="left"></el-table-column>
        <el-table-column prop="joblevel.name" label="职称" width="120" align="left"></el-table-column>
        <el-table-column prop="position.name" label="职位" width="90" align="left"></el-table-column>
        <el-table-column prop="engageForm" label="聘用形式" width="90" align="left"></el-table-column>
        <el-table-column prop="tiptopDegree" label="最高学历" width="90" align="left"></el-table-column>
        <el-table-column prop="school" label="学校" width="250" align="left"></el-table-column>
        <el-table-column prop="specialty" label="专业" width="135" align="left"></el-table-column>
        <el-table-column prop="workState" label="在职状态" width="90" align="left"></el-table-column>
        <el-table-column prop="beginDate" label="入职日期" width="90" align="left"></el-table-column>
        <el-table-column prop="conversionTime" label="转正日期" width="90" align="left"></el-table-column>
        <el-table-column prop="beginContract" label="合同起始日期" width="100" align="left"></el-table-column>
        <el-table-column prop="endContract" label="合同截止日期" width="100" align="left"></el-table-column>
        <el-table-column label="合同期限" width="90" align="left">
          <template slot-scope="scope">
            <el-tag>{{ scope.row.contractTerm }}</el-tag>年
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="190" fixed="right">
          <template slot-scope="scope">
            <el-button style="padding: 3px" size="mini" @click="showEditEmpView(scope.row)">编辑</el-button>
            <el-button style="padding: 3px" size="mini" type="primary">查看高级资料</el-button>
            <el-button
              style="padding: 3px"
              size="mini"
              type="danger"
              @click="deleteEmp(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: flex-end">
        <el-pagination
          background
          layout="sizes, prev, pager, next, jumper, ->, total, slot"
          :total="total"
          @size-change="sizeChange"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="90%">
      <div>
        <el-form ref="empForm" :model="emp" :rules="rules">
          <el-row>
            <el-col :span="6">
              <el-form-item label="姓名:" prop="name">
                <el-input
                  size="mini"
                  prefix-icon="el-icon-edit"
                  style="width: 120px"
                  v-model="emp.name"
                  placeholder="请输入员工姓名..."
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="性别:" prop="gender">
                <el-radio-group v-model="emp.gender" style="margin-top: 8px">
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="政治面貌:" prop="politicId">
                <el-select
                  size="mini"
                  style="width: 120px"
                  v-model="emp.politicId"
                  placeholder="请选择政治面貌..."
                >
                  <el-option
                    v-for="(politicid, index) of politicsStatus"
                    :key="index"
                    :label="politicid.name"
                    :value="politicid.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="出生日期:" prop="birthday">
                <el-date-picker
                  v-model="emp.birthday"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="出生日期"
                  size="mini"
                  style="width: 150px"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="民族:" prop="nationId">
                <el-select
                  size="mini"
                  style="width: 120px"
                  v-model="emp.nationId"
                  placeholder="请选择民族..."
                >
                  <el-option
                    v-for="(nationId, index) of nations"
                    :key="index"
                    :label="nationId.name"
                    :value="nationId.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="籍贯:" prop="nativePlace">
                <el-input
                  size="mini"
                  prefix-icon="el-icon-edit"
                  style="width: 120px"
                  v-model="emp.nativePlace"
                  placeholder="请输入员工籍贯..."
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="电子邮箱:" prop="email">
                <el-input
                  size="mini"
                  prefix-icon="el-icon-edit"
                  style="width: 120px"
                  v-model="emp.email"
                  placeholder="请输入员工电子邮箱..."
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="联系地址:" prop="address">
                <el-input
                  size="mini"
                  prefix-icon="el-icon-edit"
                  style="width: 150px"
                  v-model="emp.address"
                  placeholder="请输入员工联系地址..."
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="职位:" prop="posId">
                <el-select
                  size="mini"
                  style="width: 120px"
                  v-model="emp.posId"
                  placeholder="请选择职位..."
                >
                  <el-option
                    v-for="(posId, index) of positions"
                    :key="index"
                    :label="posId.name"
                    :value="posId.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="职称:" prop="jobLevelId">
                <el-select
                  size="mini"
                  style="width: 120px"
                  v-model="emp.jobLevelId"
                  placeholder="请选择职称..."
                >
                  <el-option
                    v-for="(jobLevelId, index) of joblevels"
                    :key="index"
                    :label="jobLevelId.name"
                    :value="jobLevelId.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="所属部门:" prop="departmentId">
                <el-popover
                  placement="right"
                  title="请选择部门"
                  width="200"
                  trigger="manual"
                  v-model="popVisible"
                >
                  <el-tree
                    :data="allDeps"
                    :props="defaultProps"
                    default-expand-all
                    @node-click="handleNodeClick"
                  ></el-tree>
                  <div
                    slot="reference"
                    style="
                                            width: 150px;
                                            display: inline-flex;
                                            font-size: 13px;
                                            border: 1px solid #dedede;
                                            height: 24px;
                                            border-radius: 5px;
                                            cursor: pointer;
                                            align-items: center;
                                            padding-left: 8px;
                                            box-sizing: border-box;
                                        "
                    @click="showDepView"
                  >{{ inputDepName }}</div>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="电话号码:" prop="phone">
                <el-input
                  size="mini"
                  prefix-icon="el-icon-phone"
                  style="width: 150px"
                  v-model="emp.phone"
                  placeholder="请输入员工电话号码..."
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="工号：" prop="workID" disabled>
                <el-input
                  size="mini"
                  style="width: 150px"
                  v-model="emp.workID"
                  placeholder="请输入工号"
                  prefix-icon="el-icon-edit"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="学历：" prop="tiptopDegree">
                <el-select
                  v-model="emp.tiptopDegree"
                  placeholder="学历"
                  size="mini"
                  style="width: 150px"
                >
                  <el-option v-for="item in tiptopDegrees" :key="item" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="毕业院校：" prop="school">
                <el-input
                  size="mini"
                  style="width: 150px"
                  v-model="emp.school"
                  placeholder="请输入毕业院校"
                  prefix-icon="el-icon-edit"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="专业名称：" prop="specialty">
                <el-input
                  size="mini"
                  style="width: 200px"
                  v-model="emp.specialty"
                  placeholder="请输入专业名称"
                  prefix-icon="el-icon-edit"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="入职日期：" prop="beginDate">
                <el-date-picker
                  v-model="emp.beginDate"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  style="width: 130px"
                  placeholder="入职日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="转正日期：" prop="conversionTime">
                <el-date-picker
                  v-model="emp.conversionTime"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  style="width: 130px"
                  placeholder="转正日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="合同起始日期：" prop="beginContract">
                <el-date-picker
                  v-model="emp.beginContract"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  style="width: 130px"
                  placeholder="合同起始日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="合同截止日期：" prop="endContract">
                <el-date-picker
                  v-model="emp.endContract"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  style="width: 130px"
                  placeholder="合同截止日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="身份证号码：" prop="idCard">
                <el-input
                  size="mini"
                  style="width: 180px"
                  v-model="emp.idCard"
                  placeholder="请输入身份证号码"
                  prefix-icon="el-icon-edit"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="聘用形式：" prop="engageForm">
                <el-radio-group v-model="emp.engageForm" style="margin-top: 8px">
                  <el-radio label="劳动合同">劳动合同</el-radio>
                  <el-radio label="劳务合同">劳务合同</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="婚姻状况：" prop="wedlock">
                <el-radio-group v-model="emp.wedlock" style="margin-top: 8px">
                  <el-radio label="已婚">已婚</el-radio>
                  <el-radio label="未婚">未婚</el-radio>
                  <el-radio label="离异">离异</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doAddEmp">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      emps: [],
      loading: false,
      total: 0,
      currentPage: 1,
      size: 10,
      empName: '',
      dialogVisible: false,
      nations: [],
      joblevels: [],
      politicsStatus: [],
      positions: [],
      importDataBtnText: '导入数据',
      importDataBtnIcon: 'el-icon-upload2',
      importDataDisabled: false,
      tiptopDegrees: [
        '本科',
        '大专',
        '硕士',
        '博士',
        '高中',
        '初中',
        '小学',
        '其他'
      ],
      headers: {
        Authorization: window.sessionStorage.getItem('tokenStr')
      },
      emp: {
        name: '',
        gender: '',
        birthday: '',
        idCard: '',
        wedlock: '',
        nationId: null,
        nativePlace: '',
        politicId: null,
        email: '',
        phone: '',
        address: '',
        departmentId: null,
        jobLevelId: null,
        posId: null,
        engageForm: '',
        tiptopDegree: '',
        specialty: '',
        school: '',
        beginDate: '',
        workID: '',
        contractTerm: null,
        conversionTime: '',
        notWorkDate: '',
        beginContract: '',
        endContract: '',
        workAge: null
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入员工姓名',
            trigger: 'blur'
          }
        ],
        gender: [
          {
            required: true,
            message: '请输入性别',
            trigger: 'blur'
          }
        ],
        birthday: [
          {
            required: true,
            message: '请输入出生日期',
            trigger: 'blur'
          }
        ],
        idCard: [
          {
            required: true,
            message: '请输入身份证号码',
            trigger: 'blur'
          },
          {
            pattern:
              /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/,
            message: '身份证号码格式不正确',
            trigger: 'blur'
          }
        ],
        wedlock: [
          {
            required: true,
            message: '请输入婚姻状况',
            trigger: 'blur'
          }
        ],
        nationId: [
          {
            required: true,
            message: '请输入民族',
            trigger: 'blur'
          }
        ],
        nativePlace: [
          {
            required: true,
            message: '请输入籍贯',
            trigger: 'blur'
          }
        ],
        politicId: [
          {
            required: true,
            message: '请输入政治面貌',
            trigger: 'blur'
          }
        ],
        email: [
          {
            required: true,
            message: '请输入邮箱地址',
            trigger: 'blur'
          },
          {
            type: 'email',
            message: '邮箱格式不正确',
            trigger: 'blur'
          }
        ],
        phone: [
          {
            required: true,
            message: '请输入电话号码',
            trigger: 'blur'
          }
        ],
        address: [
          {
            required: true,
            message: '请输入员工地址',
            trigger: 'blur'
          }
        ],
        departmentId: [
          {
            required: true,
            message: '请输入部门名称',
            trigger: 'blur'
          }
        ],
        jobLevelId: [
          {
            required: true,
            message: '请输入职称',
            trigger: 'blur'
          }
        ],
        posId: [
          {
            required: true,
            message: '请输入职位',
            trigger: 'blur'
          }
        ],
        engageForm: [
          {
            required: true,
            message: '请输入聘用形式',
            trigger: 'blur'
          }
        ],
        tiptopDegree: [
          {
            required: true,
            message: '请输入学历',
            trigger: 'blur'
          }
        ],
        specialty: [
          {
            required: true,
            message: '请输入专业',
            trigger: 'blur'
          }
        ],
        school: [
          {
            required: true,
            message: '请输入毕业院校',
            trigger: 'blur'
          }
        ],
        beginDate: [
          {
            required: true,
            message: '请输入入职日期',
            trigger: 'blur'
          }
        ],
        workState: [
          {
            required: true,
            message: '请输入工作状态',
            trigger: 'blur'
          }
        ],
        workID: [
          {
            required: true,
            message: '请输入工号',
            trigger: 'blur'
          }
        ],
        contractTerm: [
          {
            required: true,
            message: '请输入合同期限',
            trigger: 'blur'
          }
        ],
        conversionTime: [
          {
            required: true,
            message: '请输入转正日期',
            trigger: 'blur'
          }
        ],
        notWorkDate: [
          {
            required: true,
            message: '请输入离职日期',
            trigger: 'blur'
          }
        ],
        beginContract: [
          {
            required: true,
            message: '请输入合同起始日期',
            trigger: 'blur'
          }
        ],
        endContract: [
          {
            required: true,
            message: '请输入合同结束日期',
            trigger: 'blur'
          }
        ],
        workAge: [
          {
            required: true,
            message: '请输入工龄',
            trigger: 'blur'
          }
        ]
      },
      allDeps: [],
      inputDepName: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      title: '',
      popVisible: false,
      popVisible2: false
    }
  },
  mounted () {
    this.initEmps()
    this.initData()
    this.initPositions()
  },
  methods: {
    onError () {
      this.importDataBtnText = '导入数据'
      this.importDataBtnIcon = 'el-icon-upload2'
      this.importDataDisabled = false
    },
    onSuccess () {
      this.importDataBtnText = '导入数据'
      this.importDataBtnIcon = 'el-icon-upload2'
      this.importDataDisabled = false
      this.initEmps()
    },
    beforeUpload () {
      this.importDataBtnText = '正在导入'
      this.importDataBtnIcon = 'el-icon-loading'
      this.importDataDisabled = true
    },
    exportData () {
      this.downloadRequest('/employee/basic/export')
    },
    showEditEmpView (data) {
      this.title = '编辑员工'
      this.emp = data
      this.inputDepName = data.department.name
      this.dialogVisible = true
    },
    deleteEmp (data) {
      this.$confirm(
        '此操作将永久删除该【' + data.name + '】, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          this.deleteRequest(
            '/employee/basic/' + data.id
          ).then((resp) => {
            if (resp) {
              this.initEmps()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    doAddEmp () {
      if (this.emp.id) {
        this.$refs.empForm.validate((valid) => {
          if (valid) {
            this.putRequest(
              '/employee/basic/',
              this.emp
            ).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initEmps()
              }
            })
          }
        })
      } else {
        this.$refs.empForm.validate((valid) => {
          if (valid) {
            this.postRequest(
              '/employee/basic/',
              this.emp
            ).then((resp) => {
              if (resp) {
                this.dialogVisible = false
                this.initEmps()
              }
            })
          }
        })
      }
    },
    handleNodeClick (data) {
      this.inputDepName = data.name
      this.emp.departmentId = data.id
      this.popVisible = !this.popVisible
    },
    showDepView () {
      this.popVisible = !this.popVisible
    },
    showDepView2 () {
      this.popVisible2 = !this.popVisible2
    },
    getMaxWorkID () {
      this.getRequest('/employee/basic/maxWorkID').then(
        (result) => {
          if (result) {
            this.emp.workID = result.obj
          }
        }
      )
    },
    initPositions () {
      this.getRequest('/employee/basic/positions').then(
        (result) => {
          if (result) {
            this.positions = result
            window.sessionStorage.setItem(
              'positions',
              JSON.stringify(result)
            )
          }
        }
      )
    },
    initData () {
      if (!window.sessionStorage.getItem('nations')) {
        this.getRequest('/employee/basic/nations').then(
          (result) => {
            if (result) {
              this.nations = result
              window.sessionStorage.setItem(
                'nations',
                JSON.stringify(result)
              )
            }
          }
        )
      } else {
        this.nations = JSON.parse(
          window.sessionStorage.getItem('nations')
        )
      }
      if (!window.sessionStorage.getItem('joblevels')) {
        this.getRequest('/employee/basic/joblevels').then(
          (result) => {
            if (result) {
              this.joblevels = result
              window.sessionStorage.setItem(
                'joblevels',
                JSON.stringify(result)
              )
            }
          }
        )
      } else {
        this.joblevels = JSON.parse(
          window.sessionStorage.getItem('joblevels')
        )
      }
      if (!window.sessionStorage.getItem('politicsStatus')) {
        this.getRequest('/employee/basic/politicsStatus').then(
          (result) => {
            if (result) {
              this.politicsStatus = result
              window.sessionStorage.setItem(
                'politicsStatus',
                JSON.stringify(result)
              )
            }
          }
        )
      } else {
        this.politicsStatus = JSON.parse(
          window.sessionStorage.getItem('politicsStatus')
        )
      }
      if (!window.sessionStorage.getItem('allDeps')) {
        this.getRequest('/employee/basic/deps').then(
          (result) => {
            if (result) {
              this.allDeps = result
              window.sessionStorage.setItem(
                'allDeps',
                JSON.stringify(result)
              )
            }
          }
        )
      } else {
        this.allDeps = JSON.parse(
          window.sessionStorage.getItem('allDeps')
        )
      }
    },
    showAddEmpView () {
      this.title = '添加员工'
      this.inputDepName = ''
      this.emp = {
        name: '',
        gender: '',
        birthday: '',
        idCard: '',
        wedlock: '',
        nationId: null,
        nativePlace: '',
        politicId: null,
        email: '',
        phone: '',
        address: '',
        departmentId: null,
        jobLevelId: null,
        posId: null,
        engageForm: '',
        tiptopDegree: '',
        specialty: '',
        school: '',
        beginDate: '',
        workID: '',
        contractTerm: null,
        conversionTime: '',
        notWorkDate: '',
        beginContract: '',
        endContract: '',
        workAge: null
      }
      this.initPositions()
      this.getMaxWorkID()
      this.dialogVisible = true
    },
    sizeChange (size) {
      this.size = size
      this.initEmps()
    },
    currentChange (currentPage) {
      this.currentPage = currentPage
      this.initEmps()
    },
    initEmps () {
      this.loading = true
      this.getRequest(
        '/employee/basic/?currentPage=' +
        this.currentPage +
        '&size=' +
        this.size +
        '&name=' +
        this.empName
      )
        .then((result) => {
          this.emps = result.data
          this.total = result.total
          this.loading = false
        })
        .catch((err) => { this.$message.error(err) })
    }
  }
}
</script>

<style  scoped>
</style>
